package Teacher_Test.JDBC_test.dao.impl;

import Teacher_Test.JDBC_test.dao.UserDao02;
import Teacher_Test.JDBC_test.entity.Users;
import day01_IO_JDBC.demo_JDBC_emp_pro.DBUtil.DBPoollUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class UserDaoImpl02 implements UserDao02 {
    @Override
    public boolean add(Users users) {
        Connection connection = DBPoollUtils.getConnection();
        if (connection == null){

        }else {
            try {
                String sql = """
                        insert into users( id, username, userpwd, userage, usersex ) values(?,?,?,?,?)
                        """;
                PreparedStatement preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setInt(1,users.getId());
                preparedStatement.setString(2,users.getUsername());
                preparedStatement.setString(3,users.getUserpwd());
                preparedStatement.setInt(4,users.getUserage());
                preparedStatement.setString(5,users.getUsersex());

                int num = preparedStatement.executeUpdate();
                if (num >0){
                    System.out.println("执行成功");
                    return true;
                }
                return false;
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }
}
